package org.jsola.contract.constant;

import org.jsola.core.validator.IConst;
import org.jsola.json.JsonKit;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册类型
 *
 * @author JanusMix
 */
public enum EnterpriseUserType implements IConst {

    /**
     * 代理人注册
     */
    AGENT(1,"代理人注册"),
    /**
     * 法人注册
     */
    LEGAL(2,"法人注册");

    private final Integer value;
    private final String desc;
    /**
     * 枚举值罗列，给swagger接口文档展示用
     */
    public static final String VALUES_STR = "1,2";

     /**
     * 缓存所有value
     */
    private static final Map<Integer, EnterpriseUserType> LOOKUP = new HashMap<>();

    static {
        for (EnterpriseUserType e : EnterpriseUserType.values()) {
            LOOKUP.put(e.value, e);
        }
    }


    EnterpriseUserType(Integer value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据value查找EnterpriseUserType
     * @param value value
     * @return EnterpriseUserType
     */
    public static EnterpriseUserType find(Integer value) {
        return LOOKUP.get(value);
    }

    /**
     * 根据desc查找EnterpriseUserType
     * @param desc desc
     * @return EnterpriseUserType
     */
    public static EnterpriseUserType findByDesc(String desc){
        for (EnterpriseUserType e : EnterpriseUserType.values()) {
            if(e.getDesc().equals(desc)){
                return e;
            }
        }
        return null;
    }

    /**
     * 所有枚举转json
     * @return json
     */
    public static String getAllToJson() {
        Map<String,Object> map = new HashMap<>();
        for (EnterpriseUserType e : EnterpriseUserType.values()) {
            map.put("value", e.value);
            map.put("desc", e.desc);
        }
        return JsonKit.toJSONString(map);
    }

    @Override
    public boolean check(Object obj) {
        Integer value = (Integer) obj;
        return find(value) != null;
    }

    public Integer getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
